import { getTrainDduction, createTrain, updateTrain, getDeductionInfo } from '@/api/campus/train'
export default {
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      campusRelationListVisible: false,
      isDetail: false,
      dataForm: {
        id: '',
        trainId: '',
        deductionAmount: undefined,
        certTypeId: undefined,
        description: undefined,
        enabled: true,
        status: []
      },
      rules: {
        certTypeId: [
          {
            required: true,
            message: '请选择证书模版',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择抵扣的参训状态',
            trigger: 'change'
          }
        ],
        deductionAmount: [
          {
            required: true,
            message: '请输入优惠金额',
            trigger: ['blur', 'change']
          }
        ]
      },
      brandOptions: [],
      typeIdOptions: [],
      statusOptions: [{ enCode: 'Audit', fullName: '已报名' }, { enCode: 'Complete', fullName: '已结业' }, { enCode: 'Undergraduate', fullName: '未结业' }],
      certificateTemplateOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'TrainCertificateType' }).then(res => {
      this.certificateTemplateOptions = res.filter(t => t.enabledMark == 1)
    })
  },
  mounted() {},
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        getDeductionInfo(id).then(res => {
          if (!!res.data) {
            this.dataForm = res.data
          } else {
            this.dataForm.trainId = id
          }
        })
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.loading = true
          const formMethod = getTrainDduction
          formMethod(this.dataForm).then(res => {
            this.loading = false
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.$emit('refresh', true)
              }
            })
          })
        }
      })
    },
    closeDialog() {
      this.$refs['elForm'].resetFields()
    }
  }
}
